package com.gable.report.controller;

import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.dao.IChartsForHospitalMaintainDao;
import com.gable.report.model.ChartsBaseDataView;
import com.gable.report.model.ChartsHospitalQuery;
import com.gable.report.model.ChartsMaintainInfoView;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForHospitalBaseDataService;
import com.gable.report.service.UserService;
import com.gable.report.util.ChartsUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lvdouStart on 2017/9/22.
 */
@Controller
@RequestMapping("/charts/hospital/brand")
public class ChartsForHospitalBrandController {

    private final Log logger = LogFactory.getLog(this.getClass());


    @Autowired
    UserService userService;

    @Autowired
    ChartsForHospitalBaseDataService chartsForHospitalBaseDataService;

    @Autowired
    IChartsForHospitalMaintainDao chartsForHospitalMaintainDao;

    /**
     * @Author: 杨青桦
     * @Description: 设备厂家概览框
     * @Date: 2017/8/2 11:51
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/overview",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAllAssets(String userId, ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryAllAssetsForDeviceIndex(hospitalId, chartsHospitalQuery);
            Map<String,Object> paramMap = ChartsUtil.getParamMap(hospitalId,chartsHospitalQuery);
            ChartsMaintainInfoView maintainInfoView = chartsForHospitalMaintainDao.queryAllAssets(paramMap);
            map.put("maintainInfo",maintainInfoView);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /**
     * @Author: 杨青桦
     * @Description:  科室
     * @Date: 2017/7/27 15:34
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/department",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDepartments(String userId, ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDepartments(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  设备型号
     * @Date: 2017/7/27 15:33
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/type",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceTypes(String userId, ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceTypes(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /**
     * @Author: 杨青桦
     * @Description:  设备型号
     * @Date: 2017/7/27 15:33
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/model",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceModels(String userId, ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceModels(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description: 月份数据
     * @Date: 2017/7/20 11:42
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/year-trend",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryForMonth(String userId, ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map = new HashMap<>();
        try {
            List<ChartsBaseDataView> list = chartsForHospitalBaseDataService.queryYearTrend(hospitalId, chartsHospitalQuery);
            map.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  根据设备型号查询具体设备
     * @Date: 2017/8/2 13:40
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/device",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceInfoByModelName(String userId, ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceInfoByModelName(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

}
